package net.maku.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.system.convert.SysUserDiscountConvert;
import net.maku.system.entity.SysUserDiscountEntity;
import net.maku.system.service.SysUserDiscountService;
import net.maku.system.query.SysUserDiscountQuery;
import net.maku.system.vo.SysUserDiscountVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 用户折扣
*
* @author 小陈 www.hong.ch@qq.com
* @since 1.0.0 2025-03-26
*/
@RestController
@RequestMapping("system/discount")
@Tag(name="用户折扣")
@AllArgsConstructor
public class SysUserDiscountController {
    private final SysUserDiscountService sysUserDiscountService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('system:discount:page')")
    public Result<PageResult<SysUserDiscountVO>> page(@ParameterObject @Valid SysUserDiscountQuery query){
        PageResult<SysUserDiscountVO> page = sysUserDiscountService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('system:discount:info')")
    public Result<SysUserDiscountVO> get(@PathVariable("id") Long id){
        SysUserDiscountEntity entity = sysUserDiscountService.getById(id);

        return Result.ok(SysUserDiscountConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('system:discount:save')")
    public Result<String> save(@RequestBody SysUserDiscountVO vo){
        sysUserDiscountService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('system:discount:update')")
    public Result<String> update(@RequestBody @Valid SysUserDiscountVO vo){
        sysUserDiscountService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('system:discount:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        sysUserDiscountService.delete(idList);

        return Result.ok();
    }
}